package middleware

import (
	"github.com/gin-gonic/gin"
	"wx_build/core/jwt"
	"wx_build/core/msg"
	"wx_build/model/tables"
)

func CheckLogin() gin.HandlerFunc {
	return func(context *gin.Context) {
		userClaims, err := jwt.JwtVerify(context, "admin_token")
		if err != nil {
			msg.NotLogin(context)
			context.Abort()
			return
		}
		query := tables.User{
			UserId: userClaims.UserId,
		}
		err = query.QueryOne(&query)
		if err != nil {
			msg.NotLogin(context)
			context.Abort()
			return
		}
		context.Set("admin_user", query)
		context.Next()
	}
}
